Updated with classes and comments for clarity.