Added ASCII diagrams in comments. Code unchanged.