added KeyboardInterrupt handling