Refactored ttt code