Finally got this working properly