Initial refactoring, WIP.