Fixed a bug with self-intersection in some rare cases, and improved enclosed cycles detection.