Empty the count tables before emptying main tables to avoid causing foreign key violation errors.
Store dense features in database.
Remove code which is duplicated in TreeBuilder.
Extensive refactorying and tidying up.