Minor Changes

...unctions. also moved a couple of things into an outer scope which didnt need to be recomputed each call.