Fixed bugs and unused imports found and various issues.

This will fix several issues found via lint.