Fixed memory leak, improved output of function.