refactoring on the way. Need some more debug