Fix bug in optimizer