Refactors and fixes from Marshall.