FIX: Possible double free errors in some cases.