Probably cleaner/simpler to follow if this was a global class, not defined inside a function.