Refactor line detection