Current implementation requires a lot of stack space. It's make sense to optimize it somehow, may be replace recursion with iteration.