Fixed various issues in tempate resource merge