WIP
Code cleanup and refactoring.