cleaned code up and made it work with objects