YL: refactored some code