Various bug fixes and improvments.