Fix bugs found in testing