Added debugmode and graceful exit.