clean up
refactor the API of 'attach' function.