Implementing tests and fixing issues found, see #1