Fix arrow key bug in specific element mode + code refactoring