Small refactoring and fixes