re-organized several functions in mergewaypoints