Restore old algorithm + refactoring
Tweaked algorithm again + refactoring
Removed unnecessary code
Merge pull request #1 from ahon1/ref

Refactoring