Simple refactoring and bug fixes