The provided script functions properly. There was no error and everything worked straight out of the box. However, your explanation of what the script is doing is not entirely correct. Actually all points are correct except point 3. Point 3 is completely unrelated to the script you have provided. Therefore, it should be removed from your reply.