General refactoring and cleaning up.