modified some functions and added line detection