refactoring, segmetation fault fix, bags in tests fix