updated curly brackets indentation, and exiting each loop as soon as a header doesn't match