General cleanup and refactoring.