Clean up and code reorganization