Refactoring finished. Some robustness added. Pre-test commit.