Code cleanups.
Fixed some possible bugs.