reorganize code to save some fp multiplications using iterate(), name index 'n' consistently
Merge branch 'master' into mac