API reimplemented from scratch; console I/O functions; various bug fixes