Various tweaks and refactoring