Fix minor issues and refactoring