major refactor in advance of adding bindings. perf improvements for dirty-check cases