diff --git a/apps/specular_estimation/CMakeFiles/specular_estimation.dir/src/specular_estimation.cc.o b/apps/specular_estimation/CMakeFiles/specular_estimation.dir/src/specular_estimation.cc.o
index 9ecf6cab6cfa8cdfd8ae23ec22cc01d1b1e52bd8..83fa06c5d01d43e347783e2015730253a3a014f9 100644
Binary files a/apps/specular_estimation/CMakeFiles/specular_estimation.dir/src/specular_estimation.cc.o and b/apps/specular_estimation/CMakeFiles/specular_estimation.dir/src/specular_estimation.cc.o differ
diff --git a/apps/specular_estimation/src/Ceres.h b/apps/specular_estimation/src/Ceres.h
index 35019ecad6a2c6ce3120abe3c97cef5c6d5c44ac..96407de07b758a5f6994c176088927f91b2bd8d4 100644
--- a/apps/specular_estimation/src/Ceres.h
+++ b/apps/specular_estimation/src/Ceres.h
@@ -38,8 +38,10 @@ class MyScalarCostFunctor {
 				glfwSwapBuffers(window);
 				glfwPollEvents();
 			}
+
+			//std::cout << "Residual = " << (sum/numberOfLights_) << std::endl;
 			
-			residualValue[0] = sum;
+			residualValue[0] = (sum/numberOfLights_)*(sum/numberOfLights_);
 			//residualValue[0] = function(SpecularIntensity, SpecularPower);
 			return true;
 		}
@@ -98,7 +100,7 @@ void specularMinimisation(double& SpecularIntensity, double& SpecularPower, doub
 
 	problem.SetParameterLowerBound(&SpecularIntensity, 0, 0);
 	problem.SetParameterUpperBound(&SpecularIntensity, 0, 1);
-	problem.SetParameterLowerBound(&SpecularPower, 0, 1);
+	problem.SetParameterLowerBound(&SpecularPower, 0, 0.1);
 
 	// Run the solver!
 	Solver::Options options;
diff --git a/apps/specular_estimation/src/OpenGL.h b/apps/specular_estimation/src/OpenGL.h
index 2a95e16e70173a46f97578759c7bd7067d71da52..6a9fb74400c45fceb64c7477d12da5b16adc0fc6 100644
--- a/apps/specular_estimation/src/OpenGL.h
+++ b/apps/specular_estimation/src/OpenGL.h
@@ -490,7 +490,7 @@ void computeResidual(int imageNumber, int windowHeight, int windowWidth, std::st
 	
 	// Create an empty Mat the same size as the image
 	cv::Mat temp = cv::Mat(windowHeight, windowWidth, CV_8UC3);
-	cv::Mat sumOfSquaredDifferences = cv::Mat(windowHeight, windowWidth, CV_16UC3);
+	cv::Mat sumOfSquaredDifferences = cv::Mat(windowHeight, windowWidth, CV_16SC3);
 	
 	// Read the image into the Mat as an 8-bit colour image
 	glReadPixels(0, 0, windowWidth, windowHeight, GL_BGR, GL_UNSIGNED_BYTE, temp.data);
@@ -501,13 +501,15 @@ void computeResidual(int imageNumber, int windowHeight, int windowWidth, std::st
 	//temp = temp(cv::Rect(1, 1, windowWidth-2, windowHeight-2));
 	//copyMakeBorder(temp, temp, 1, 1, 1, 1, cv::BORDER_REPLICATE);
 	
+	/*cv::Mat texture = textureImages[imageNumber];
+	cv::cvtColor(texture, texture, CV_BGR2GRAY);
+	texture.convertTo(texture, CV_8UC3);*/
+
 	// Calculate the absolute differences between the model and the photograph
 	cv::absdiff(temp, textureImages[imageNumber], residual);
 	
-	sumOfSquaredDifferences = residual;
-	
 	// Square each element
-	multiply(sumOfSquaredDifferences, sumOfSquaredDifferences, sumOfSquaredDifferences);
+	multiply(residual, residual, sumOfSquaredDifferences);
 	
 	std::ostringstream stm;
 	stm << imageNumber;
@@ -524,8 +526,9 @@ void computeResidual(int imageNumber, int windowHeight, int windowWidth, std::st
 	// Crop a 1-pixel border around the residual
 	sumOfSquaredDifferences = sumOfSquaredDifferences(cv::Rect(1, 1, windowWidth-2, windowHeight-2));
 	
+	sum = (cv::sum(residual)[0] + cv::sum(residual)[1] + cv::sum(residual)[2])/(3 * (windowHeight-2) * (windowWidth-2));
 	//sum = (cv::sum(sumOfSquaredDifferences)[0] + cv::sum(sumOfSquaredDifferences)[1] + cv::sum(sumOfSquaredDifferences)[2])/(windowHeight-2 * windowWidth-2);
-	sum = (cv::sum(sumOfSquaredDifferences)[0] + cv::sum(sumOfSquaredDifferences)[1] + cv::sum(sumOfSquaredDifferences)[2]);
+	//sum = (cv::sum(sumOfSquaredDifferences)[0] + cv::sum(sumOfSquaredDifferences)[1] + cv::sum(sumOfSquaredDifferences)[2]);
 	residuals = cv::Vec3d(cv::sum(sumOfSquaredDifferences)[0], cv::sum(sumOfSquaredDifferences)[1], cv::sum(sumOfSquaredDifferences)[2]);
 	
 	
@@ -540,11 +543,12 @@ void computeResidual(int imageNumber, int windowHeight, int windowWidth, std::st
 	//std::cout << "Image " << imageNumber << ", average sum of squared differences: R = " << sum << ", specular intensity = " << SpecularIntensity << ", specular power = " << SpecularPower << std::endl;
 }
 
-void computeResidual(int imageNumber, int windowHeight, int windowWidth, std::vector <cv::Mat> textureImages, double& sum) {
+void computeResidual(int imageNumber, int windowHeight, int windowWidth, std::vector<cv::Mat> textureImages, double& sum) {
 	
 	// Create an empty Mat the same size as the image
 	cv::Mat temp = cv::Mat(windowHeight, windowWidth, CV_8UC3);
-	cv::Mat sumOfSquaredDifferences = cv::Mat(windowHeight, windowWidth, CV_16UC3);
+	cv::Mat residual = cv::Mat(windowHeight, windowWidth, CV_8UC3);
+	cv::Mat sumOfSquaredDifferences = cv::Mat(windowHeight, windowWidth, CV_16SC3);
 	
 	// Read the image into the Mat as an 8-bit colour image
 	glReadPixels(0, 0, windowWidth, windowHeight, GL_BGR, GL_UNSIGNED_BYTE, temp.data);
@@ -552,17 +556,23 @@ void computeResidual(int imageNumber, int windowHeight, int windowWidth, std::ve
 	// 3D models have the origin in the bottom-left corner, while images have the origin in the top-left corner. The image is flipped to convert between the two.
 	cv::flip(temp, temp, 0);
 	
+	/*cv::Mat texture = textureImages[imageNumber];
+	cv::cvtColor(texture, texture, CV_BGR2GRAY);
+	texture.convertTo(texture, CV_8UC3);*/
+
 	// Calculate the absolute differences between the model and the photograph
-	cv::absdiff(temp, textureImages[imageNumber], sumOfSquaredDifferences);
+	cv::absdiff(temp, textureImages[imageNumber], residual);
 	
 	// Square each element
-	multiply(sumOfSquaredDifferences, sumOfSquaredDifferences, sumOfSquaredDifferences);
+	multiply(residual, residual, sumOfSquaredDifferences);
 	
 	// Crop a 1-pixel border around the residual
+	residual = residual(cv::Rect(1, 1, windowWidth-2, windowHeight-2));
 	sumOfSquaredDifferences = sumOfSquaredDifferences(cv::Rect(1, 1, windowWidth-2, windowHeight-2));
 	
+	sum += (cv::sum(residual)[0] + cv::sum(residual)[1] + cv::sum(residual)[2])/(3 * (windowHeight-2) * (windowWidth-2));
 	//sum = (cv::sum(sumOfSquaredDifferences)[0] + cv::sum(sumOfSquaredDifferences)[1] + cv::sum(sumOfSquaredDifferences)[2])/(windowHeight-2 * windowWidth-2);
-	sum += (cv::sum(sumOfSquaredDifferences)[0] + cv::sum(sumOfSquaredDifferences)[1] + cv::sum(sumOfSquaredDifferences)[2]);
+	//sum += (cv::sum(sumOfSquaredDifferences)[0] + cv::sum(sumOfSquaredDifferences)[1] + cv::sum(sumOfSquaredDifferences)[2]);
 }
 
 double returnResidual(int imageNumber, int windowHeight, int windowWidth, std::vector <cv::Mat> textureImages, cv::Mat& residual, cv::Vec3d& residuals, double SpecularIntensity, double SpecularPower) {
diff --git a/bin/specular_estimation b/bin/specular_estimation
index 7cecd8ab6fc2f20a5dd472b69c8e3fb897ec1800..ee2df2d4eb34c1c2f4ec9ca556084b32da22576e 100755
Binary files a/bin/specular_estimation and b/bin/specular_estimation differ