Made closing function better

Also added DEBUG logging when '-debug' command-line argument is present