Moved repetitive code in the function