Some code cleanup
Fix a bug in test files