Refactoring and tests. #kon-15 done