Reworking this implementation, it was really slow.