Converted raw code to two functions