tctc: rework code and fix memory leak

Signed-off-by: Luka Perkov <luka.perkov@sartura.hr>