Rearrangement of callback functions to follow flow

Also added exit condition flag per mpenick suggestion