Optimized the code to be faster and in fewer lines.