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