fix corner case for end detection