updated Greg's code