refactoring run function