Adding tests, minor bug fixes