Still working on fixing the merging part in the AFM loop. Progress, but still something wrong.