chain replace robust, speed up ref.id assignment, 

Chain replace now works by changing chain letter to assign when atom
number returns to 0. ref.id assignment sped up by calculating length
of data first (n) then using that as while loop.