Fixed hash integrity checking + tests + refactoring