Added speed up for the code by a factor of 40 to 80