The next few comments are more or less me walking through it myself (comments in code rock yo)
(also an explanation of why this is done and what would happen if the assumption of what "end" is isnt true)