Added some optimizations that seem to have sped things up significantly