code-cleanup: improved data structure, rewrote much of the original code