Added outright paranoid sanity checks when freeing resources. Cleaned up trailing whitespace.