[core] Remove unused imports and dead code.
[setup] Add a __main__ guard.

So that the module can be imported for testing.