improve some logic