Improved script for handling this task