Added total and did some refactoring.