Adding tests, minor code improvements.