refactored runOnRow logic into a function