fixed error that x1 > x2 might happen