final sript with functions