fixed lots of stuff

put stuff into closure to reduce scope. should work