Restructured for easier testing