Correctly check for malformed file
Refactor main