Refactored and removed duplicated code