Added extensive tests. Fixes #2