Misc. Bug fixes and optimization.