improved match beginning and ending