some refactoring to handle total better