Making some optimizations to reduce running time by half