fixing up some suboptimal code
fixed errors