Cleanup cleanup =P

Properly halt and cleanup threads, consolidate the rest of the exiting
logic into a function.