Minor refactoring and fixes