Some optimization fixes