Fixed lots of bugs, added better test