Fixed scope issue with total.
Removed debug statements. Cleaned up code slightly.
Some planning