changed making calculation to be faster and a bit more readable (with room for improvement)