fix div by zero bug (+other minor things)