Revised code
Made minor refactorings to simplify code