Finally fixed the race condition in the input handler