Some fixes + cleanup.

These fixes still need to be tested.