Incorect algorithm. Will refactor