Refactored for readability, removed partial support for trd files.