Fix bugs for corner cases