Trying to fix this. Imported some code from another project
It's a little better, but still a huge mess