refactoring fuction.