General code cleanup; introduced some structure to code