std::string causes segfaults, so use C-style string handling instead. Also hugely reduces number of times that memory is copied