Fixed bug with self loops