improved generate truth table function