Memleak fixed. Errors probably cause memleaks, better error handling needed