Fix some bugs found in testing