First pass at refactoring to use continuations