refactor: remove duplicate code
fixed key issue in each, added tests