Speed improvements, removing unnecessary code and replacing divisions

Removed some unnecssary operations, factored code in order to perform computation only if the data could have changed, used multiplications instead of division where possible