fixed division by zero
fixed some bugs