split main function in smaller functions
refactor code