Avoid algorithm crash on border. Fixed #1