optimize some code around merge

Optimize some code around `merge`, also drop extra variables while doing so.