more changes to code to make more readable and modular