Consolidated error checking in getindex()