Some optimization and code clean up.