Updated implementation from a complete fuckup to (hopefully) correct one, updated docs.