converted to c, improved compatibility