This allows to run more than one instance of the script in parallel. Also, properly clean up on exit.