Misc refactoring improvements