Refactoring the code using .stub and let