new version that works for n-dimensional arrays (or several of them)