Fixing a bunch of bugs. FIrst version now working \o/