refactored run function