convert to pure C (probably needs more fixing until fully functional)