Script refactoring

Bug correction