Replaced std::cout with faster printf(). Fixed some memory leaks.