Fixed and refactored scripts