error handling
fixed a lot of memory leaks