refactoring - split the code to more functions