Refactor with proper mocking