Fix crashes/memleaks