Add documentation and fix counting bug.