revised and corrected code