Fix some magic numbers and ensure pointers are set to NULL after free()