Refactoring and small improvements