Optimization of the code. Now it should run significantly faster.