a lot of cleanup and refactoring