fix issue #1315

fix wrong condition, and little code reorganize: static methods, instance methods