Slow implementation but works