refactored to removed duplicated code