Complete reorganization with a bunch of bug fixes