Related #293, Bugs fixes for corner cases