lots of fixes for different unusual cases