Added frees to the code. No leaks are possible at this point