simplified algorithm, from the first time, it seems, that it could work :)