added cleanup function; added KeyboardInterrupt exception