refactoring and simplify iap code