update p to current pointer for easier understanding