fix bug with total diff
Some progress in refactoring.