fixed up and condensed some code