Return an error instead of panicking
Add tests for errors (fixes #5)