rewritten to C