Various bug fixes and rebase conflicts