Refactor `run` function