adding in partial caching and...

slowly improving code to remove global variables