Clear up unused imports

Static analysis revealed a load of things that are imported but not used. I've tried to leave things that look like part of the API, and clear up other things.