Rewrote a lot of code. Too tired to explain every change. But now it's better and mostly works.