Hi frederik30, welcome.
"That doesn't work as expected", what does that mean ?
You should have a look at what you expected, and at what you got instead.
Then you should read the code you created, and have a real good look to find whether you can explain why the code does what is does, instead of what you expected it to do.
I'm sure the code does exactly what you have told it to do, so your expectations must be wrong..
By the way, you forgot to put the code in the code tags.
Take some time to read the forum manual (clcik !) and find out how to do that.