refined logic for clarity and address potential issue