Fix severe memory leak on Linux, and some other minor stuff