Updated code according to Nir's comments