reorganized code for line detection