fix needle function by refactoring