Refactoring and adding new functions