Futher cleanup and refactoring