refactoring
refactoring and add basic error handling in script