prevent memleak and fix indentation a bit
NULL-initialize some vars to avoid logic flaws in the code below