Added counting and small optimisation for the last few boxes