More tweaks and refactoring