refactor and add function