Fix boundary check bug