Added checks to avoid devision by zero