updated script with some fixes