Comments are moderated. It may take a few minutes before your comment appears.
Markdown is supported in your comments.
Let's remake it using closures. Closures, in a nutshell, allow functions to be defined on the fly while remembering the function's scope. Note this code already includes the defensive variable swap.