tried to implement algorithm