rewrote readdir to be function-based

replaced nested try/except with sub-functions