add out-of-bound check