Code cleanups and make code a bit more robust.
Fix active flag when patching.