Refactor and remove function