Refined comments and syntax, refactored reassignment of $key once one is removed