Better file types detecting.
Add some comments and cleanup.