Several optimizations. Dropped no less than 15 seconds of running time