Split up deinit code to deinit_program function