Made algorithm more clear