Small script structure refactor to make it more testable/reusable.