optimize some code around merge