changed expanding algorism slightly