SICP has a great chapter with an example of how to modularize an entangled code:
Chapter 2.2.3: Sequences as Conventional Interfaces,
http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-15.html...
"The value of expressing programs as sequence operations is that this helps us make program designs that are modular, that is, designs that are constructed by combining relatively independent pieces."