Fix global test errors, code refactor