modifed and refactored script