refactored Huizhus code