Moved loop to run function