Converted code to function