Refactor line graph functions to remove duplicated code