Working refactored version of romains algo
Clean refactoring worked