First fix for #33, still a few more bugs to kill
Add more tests