Reworked/fixed much of the logic in parse_file