fixed up some code and made it work better