Cleanup and split into modules