Refactoring the add function