It works, but the code is kind of ugly. Whatever.