Code refactoring #98 #97

Move duplicated code into unique function