Much refactoring & cleanup.