refactored some lines and separated functions in a separate file