Refactoring and some utilities