Fix functioning on focus, optimise code, refactor code