refactored and working lend function