Bugfix and implemented a bunch of tests