Faster surrounding function (removed use of delete and nub).