Refactoring some scripts