Minor code improvements

Now the program can exit on key event.
Solved a problem in which the program has a delay on execution of a few
secs.