updated code from Gal