fix bugs in counting input and output; minor changes to make