Added matrix indexing checks