Refactor out imperative logic to `SubRoutines'