Add range checking for array plus some exception handling