refactored, added some object-oriented logic