Cleanup and split main.