advanced template matching