More unit tests and corresponding bug fixes