add extra conditions, fix bugs