Restore tests and various bug fixes.