new version

massive rewrite
code cleanup
formatting changes