Major cleanup and many fixes.