Added more functions, maybe solved some memory leaks.