Added more robust check against division by zero