Initial working copy (somewhat polished, has insane memory leaks which are mostly on purpose)