Small refactoring and optimalization