fixed C implementation