added support for arbitrary number of dimensions