Some bug fixes and pre-work on caching.