Optimizer: fixes and new tests