Add boundaries check to prevent ordinal out of range error