General code cleanup and refactor.