Good catch. I had my logic entirely messed up here! Rewritten, and comments added.