refactored spaghetti code into object-oriented-ish code