code works, probably a lot of memory leaks to fix