Main bug fixes