Tried to make algorithm