Refactoring done. Now tweaking algo