1. do some refactoring
2. fix the related tests