Slightly reworked the code for readability