Fixed some bugs found in testing.