Minor refactoring and code cleanup