Code cleanup and refactoring.