Reorganized to use classes more effectively