Some cleaning up and refactoring