Code refactorization

Code refactorization. Still need to refactor checking  algorythm.