Fixed bug in Triangle function + algebraic simplification.

When testing the library I noticed that the Triangle function was broken after code re-factoring.