Unify all focus code

It used to be all over the place and using tons of undocumented variables.
This commit is quite intrusive and change a lot of things (hopefully
for the better). Please report any regression.