New version

Splited logic.
Fix couple of bugs.