you could refactor to multiple functions that use trampoline
to call the next - with that refactor you wouldn't need a ref either (effectively using continuation passing)
or even just cond-> instead of ifs to chain the updates now that I read more closely