refactor code to make more modular