Fixing various modal/focus related bugs

This fixes the following issues:

#4809