Converted redundant code to function