Testing based changes.

Fixed a lot of errors found while testing. Still has problems.