modified as per Ricardo's suggestions