refactor code to be more modular and readable