Focus fixes

This fixes some issues surrounding keyboard focus - see commit notes for more details.