Big fat rework

A really huge rework, because the previous version was taking way too much
memory.
And also, because I was too dumb to commit in between.

I still have to test it, and refactor the code some more, because it's not as
clean as I would like it to be.
It should be okay to use though.