This code also works. But I can't figure out how could I tweak it to match my needs and implement it in my project.