Fix bugs in unit test and condense script