first draft of rev. c with switchable input/output