add boundary check