fixed the logic of the script