betweennes code cleanup.

 - removed nested structures.
 - iterative version of counting.
 - updated test.
 - added comments.