cleaned up and commented triangle detection