rewrite using loop and recur, fixes #2