fixed broken NIF memory and thread handling