Major refactoring to use classes