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 8e78d573493f2d4d00a4408bef939ca0f3e1c0d2..dec791fb80424a625a03d3fa21e9cbc20807488d 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/OpenGL.h b/apps/specular_estimation/src/OpenGL.h
index 6b4b43fbd63c2d633fa8d3281fe532f714aecebd..2154ba679cc2922dc9011e2c8bf215ccf879367e 100644
--- a/apps/specular_estimation/src/OpenGL.h
+++ b/apps/specular_estimation/src/OpenGL.h
@@ -485,7 +485,7 @@ void computeResidual(int imageNumber, int windowHeight, int windowWidth, std::ve
 	//cv::imshow("Residual", residual);
 	
 	//
-	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])/(windowHeight * windowWidth);
 	residuals = cv::Vec3d(cv::sum(sumOfSquaredDifferences)[0], cv::sum(sumOfSquaredDifferences)[1], cv::sum(sumOfSquaredDifferences)[2]);
 	
 	
@@ -496,7 +496,7 @@ void computeResidual(int imageNumber, int windowHeight, int windowWidth, std::ve
 	sum = cv::sum(residual)[0];
 	*/
 	
-	std::cout << "Sum of squared differences for image " << imageNumber << " = " << sum << ", specular intensity = " << SpecularIntensity << ", specular power = " << SpecularPower << std::endl;
+	std::cout << "Image " << imageNumber << ", sum of squared differences per pixel: R = " << cv::sum(sumOfSquaredDifferences)[2]/(windowHeight * windowWidth) << ", G = " << cv::sum(sumOfSquaredDifferences)[1]/(windowHeight * windowWidth) << ", B = " << cv::sum(sumOfSquaredDifferences)[0]/(windowHeight * windowWidth) << ", specular intensity = " << SpecularIntensity << ", specular power = " << SpecularPower << std::endl;
 }
 
 double returnResidual(int imageNumber, int windowHeight, int windowWidth, std::vector < cv::Mat > textureImages, cv::Mat& residual, cv::Vec3d& residuals, float SpecularIntensity, float SpecularPower) {
@@ -523,7 +523,7 @@ double returnResidual(int imageNumber, int windowHeight, int windowWidth, std::v
 	//cv::imshow("Residual", residual);
 	
 	// 
-	double sum = cv::sum(temp)[0] + cv::sum(temp)[1] + cv::sum(temp)[2];
+	double sum = (cv::sum(temp)[0] + cv::sum(temp)[1] + cv::sum(temp)[2])/(windowHeight * windowWidth);
 	residuals = cv::Vec3d(cv::sum(temp)[0], cv::sum(temp)[1], cv::sum(temp)[2]);
 	
 	
@@ -534,7 +534,7 @@ double returnResidual(int imageNumber, int windowHeight, int windowWidth, std::v
 	sum = cv::sum(residual)[0];
 	*/
 	
-	std::cout << "Sum of squared differences for image " << imageNumber << " = " << sum << ", specular intensity = " << SpecularIntensity << ", specular power = " << SpecularPower << std::endl << std::endl;
+	std::cout << "Average sum of squared differences for image " << imageNumber << " = " << sum << ", specular intensity = " << SpecularIntensity << ", specular power = " << SpecularPower << std::endl << std::endl;
 	
 	return sum;
 }
@@ -588,6 +588,8 @@ void computeResiduals(cv::Vec3d& residual, std::vector< cv::Vec3d >& residuals,
 		lightNumber++;
 	} while (lightNumber < numberOfLights);
 	//} while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && glfwWindowShouldClose(window) == 0); // Check if the ESC key was pressed or the window was closed
+	
+	lightNumber = 0;
 }
 
 void viewModel(cv::Vec3d& residual, std::vector< cv::Vec3d >& residuals, double& totalResidual, cv::Mat& residualImage, bool calculateResidual, glm::mat4 depthProjectionMatrix, glm::mat4 depthViewMatrix, int width, int height, glm::vec3& position, float& horizontalAngle, float& verticalAngle, float& FoV, glm::vec3& lightInvDir, cv::Mat lightDirections, std::vector< cv::Mat > textureImages, int& lightNumber, int numberOfLights, GLuint& SpecularIntensityID, float& SpecularIntensity, GLuint& SpecularPowerID, float& SpecularPower, GLuint programID, GLuint ModelMatrixID, GLuint ViewMatrixID, GLuint DepthBiasID, GLuint lightInvDirID, GLuint Texture, GLuint TextureID, GLuint depthTexture, GLuint ShadowMapID, GLuint vertexbuffer, GLuint uvbuffer, GLuint normalbuffer, GLuint elementbuffer, std::vector<unsigned int> indices, GLuint MatrixID) {
@@ -696,6 +698,9 @@ void computeMatricesFromLights(int windowWidth, int windowHeight, glm::vec3& pos
 			calculateResidual = true;
 		}
 	}
+	else {
+		lightInvDir = glm::vec3(lightDirections.at<float>(lightNumber, 1), -lightDirections.at<float>(lightNumber, 0), lightDirections.at<float>(lightNumber, 2));
+	}
 	
 	// Modify specular power
 	if (glfwGetKey(window, GLFW_KEY_7) == GLFW_PRESS) {
diff --git a/bin/specular_estimation b/bin/specular_estimation
index 69f0530dea37d00c8125baa5de83c250107c09c5..4a95a202c60708a5dad29013c3a0586597fb01b3 100755
Binary files a/bin/specular_estimation and b/bin/specular_estimation differ