optimized the code so we dont have to do so many calculations