optmize alignment algorithm