Refactored to use prototype based inheritance and objects. It runs slower this way, but the code is cleaner.