Refactor tm_test.py