The loop was right, but the following if was meant to check if the token was terminated correctly so it needs to be *p.