This should be before using $lastPos as the first one might be different from 0