some cleanup and refactoring