Refactor some intermediate code