Rewrote write function to be independent of #dimensions.