Re-wrote function and fixed infinite bug
Removed underscore dependency