Changed shutdown function be a function on the module context, for easier testing.