crude implementation
did a pseudo-optimisation, runs way slower but is more generic than before
problem description