merging changes from latest code from Nathan