more functions and refactoring