Add debug log in test function. Reformat some code.