refactor and atomic function