Refactoring and cleanup

- Work-in-progress, won't compile.