refactored a function by spliting it into two