General code cleanup and refactor.
Merge pull request #1 from aleph2012/code-cleanup

General code cleanup and refactor.