code adapted for older PHP version