This needs more testing, but it looks like a valid refactor of redeal.