Cleaned up and corrected code