Fixed nasty variable scoping issues by writing a parameterized class.