initial working version of algorithm