moving functionality to own file
some refactoring here