Speed improvements, removing unnecessary code and replacing divisions by multiplications