added find closest element algorithm