Solution works but is slow, maybe needs to be sped up.