Fix some memory leaks and whitespace cleanup