Refactored scripts, fixed some problems