Working, albeit not great, solution