generalize script to fields of arbitrary dimension