Bug fixes for optimization