Revised and formatted code