undo bogus bugfix
merge in improvements from stan moore
flatten outer loop and use more specific template arguments
remove unused variable definitions