Fix possible division by 0 errors