Fix obvious bug, some script refactoring
Fix another script bug