refactorization of loopfinder - fully working