Cleanups and refactoring