Various bug fixes