working version of algorithms