bugfixes. code is still a massive mess.