shortened it up a bit and removed some uneccessary memory allocations