Fixed a few errors, optimizations.