implemented my algorithm