Minor refactoring and fixing