added some matrix math optimizations