fix counting of units, making it slower but more correct