refactored to class and corrected