improved version, cleaner, less error-prone scripts