Refactor code to be more modular