Minor refactoring and clean up of code