refactored code into a clean function loop