Copied and pasted from jampot5000's fork. Something broke and i don't have the patience to find out what.