Fix all the memory issues (other than some still reachable memory in ncurses, but there's not much we can do about that)