optimisation: conversion to a hash was moved outside the while loop since it only needs to be created once