Loki may not be as horrible of a villain as we thought. The Trickster god may not have been in full control when he invaded Earth in the first Avengers film. Let's take it back to the first Thor. In the film, Loki learns that he's adopted and half ice-giant. His anger and pain caused him to scheme for the throne, which should belong to Thor. After a failed attempt to take over, Loki falls from the Bifrost and seemingly dies. Then, He reappears in Avengers after falling from the Bifrost into another dimension where he meets The Other and the Chitauri.
This is also where Loki and Thanos get acquainted and he gives Loki a Chitauri army and the Mind Stone scepter (this happens off-screen). Marvel's official website has updated that specific part of MCU canon to include that the Mind Stone was secretly bending Loki's will to Thanos' intentions the whole time. The excerpt from their site reads:
"Arriving at the Sanctuary through a wormhole caused by the Bifrost, Loki met the Other, ruler of the ancient race of extraterrestrials the Chitauri, and Thanos. Offering the God of Mischief dominion over his brother’s favorite realm Earth, Thanos requested the Tesseract in return. Gifted with a Scepter that acted as a mind control device, Loki would be able to influence others. Unbeknownst to him, the Scepter was also influencing him, fueling his hatred over his brother Thor and the inhabitants of Earth."
This would explain Loki's wavering loyalty, and also his confusion in the first Avengers film where he states: "I remember a shadow. Living in the shade of your greatness. I remember you tossing me into an abyss, I who was and should be king." Apparently, the Mind Stone had Loki believing that Thor stole the throne from him and tossed him into space, which isn't what happened.