I don't know if the code is correct, but at least I tested it and it do the work.