some useful algorithms