Further cleanup and code reorganization.