refactor to make algorithm more clear