Fix minor usability bugs

Elements for add dialog are sorted only once in the beginning now to
avoid trouble with indices later.

Fixed focus control bugs when opening and closing dialogs.