fixed bug in optimization script, should work now