Working code without multithreading. Did not free anything