changes to Max's migrate script
merged from stuart branch