First version of fixed script. All parameters are configured inside. Please do not run until you understand what it's going to do.