refactoring, bugfixing, adding solution for working with rest