Loki may not be as bad as a criminal as we thought. The God of Trickster might not be in full control when he invaded Earth in the first Avengers film. Let's go back to the first one Thor. In the film, Loki learns that he was adopted and half a giant of ice. His anger and pain caused him to plan to take the throne, which should belong to Thor. After a failed attempt to take over, Loki fell from Bifrost and appeared to be dead. Then, He reappeared Avengers after falling from Bifrost to another dimension where he met The Other and Chitauri.

This was also the place where Loki and Thanos got acquainted and he gave Loki the Chitauri troops and the Batu Pikir stick (this happened outside the screen). The official Marvel website has updated a certain part of the MCU canon to include that Mind Stone quietly bent Loki's will towards Thanos's intentions all the time. Excerpts from their site read:

"Arriving at Sanctuary through a wormhole caused by Bifrost, Loki met the Other, ancient extraterrestrial ruler Chitauri, and Thanos. Offering god Deity Mischief over his brother's favorite region of the Earth, Thanos asked Tesseract in return acting as a mind control tool, Loki will be able to influence others. Unbeknownst to him, the Royal Wand also affected him, triggering his hatred for his brother Thor and the inhabitants of the Earth. "

This will explain Loki's faltering loyalty, and also his confusion at the first Avengers the film in which he stated: "I remember the shadow. Live in the shade of your greatness. I remember you throwing me into the abyss, I was the one and should be king." Apparently, Stone of Mind made Loki believe that Thor stole the throne from him and threw it into space, and that was not what happened.