replaced matrix and ArrayList with HashMaps, much simpler to follow now