A bit of code cleanup and refactoring