Refctored duplicate code