Refactor step function