Fix bug in metadata writing. Allow metadata at top.

@stoicflame please review