tfe: endian fixes and code optimization