Fixes #114. Added detection of corrupted config files. Plus some minor refactoring. 

Fixes #114