corrected peek algorithm