Refactoring and other minor changes