Some restructuring and fixes.