fix denominator zero and a bug