External optimizer working I think. MESSY code