create class and move some functionality to it
some correction and improves
refactoring