fix repeat of test; remove unused variable by use of each_key

1. The loop was unnecessarily repeating itself
2. Removed unused variable by use of each_key in place of each