[BenweiZhu] extract the complex condition logic to method
[BenweiZhu] remove some unused or extra variable