Imported Kevin's solution. Continued implementation