Fixed @Xyene's code and seems to be working nicely!