Refactor to split functions in different files