code refactoring to fix api bug