Bug fixes for corner cases