big cleanup and refactoring