fixed crash by adding range check