Refactoring functions, separating main and added Threshold Function