Tested on linux, fixed some leaks