Messy API implementation but it works