Refactored, new class introduced for cleaner code