Fixed bug in optimization