Refactor and cleanup.

- Add error handling for parsing error.
- Add comments for each function.
- Split function parse_log() into several small functions.