cleanup io, replace C-style memory allocation with C++ one