Code cleanup and refactor.