refactored a bit, algorithm is now working from curves as well