Refactor the code and separate it into 3 files