The 19-year-old man accused of shooting 17 people to death at a Florida high school legally purchased the assault rifle used in the killings and may have foreshadowed the attack in a social media comment investigated by the FBI last year, authorities said Thursday.

New details about the troubled background and gun ownership of the suspect, Nikolas Cruz, emerged as he appeared in court to face formal charges of carrying out the second-deadliest mass shooting at a public school in U.S. history.

Authorities also shed more light on how the suspect made a getaway by blending in with students fleeing the school, then casually spent more than an hour drifting through a Walmart store and visiting two fast-food outlets before he was arrested.