Tidy parsing logic