Fix bugs similar to #1340