Form autofocus and some refactoring