Fixed a couple of bugs, still need to test extensively