Looks like some check for the state of $RES variable is missing here