fixed some memory inefficent code