refactored ip func