Fixed some bugs.

Still a lot to fix.