Rewrite the module

Much simpler code now. Also fixed some bugs and unused code. Rewrite the tests to be simpler. And fixed code style and other things.