Fix potential memory leak found by Ethan