Major tidy-up. TODO: incremental rebuild