the code itself is actually pretty long, but at least it works!