Added shutdown hook for mctd
refactored mctd main thread