Add cleanup functions and properly propagate shutdowns