Minor fixes and refactoring