Simple solution for the trinary problem
Refactored and cleaned up the code
Further refactoring of the trinary problem