Looks horrible, the code is nonsense, but it works