Fixed crash and memleaks