Alternate solution for #6

Based on your solution, tried to simplify/clarify. Not claiming it's really much better, though. Perhaps using closures instead of global variables would actually be better. What do you think ?