Merged code from @benmay, would have liked to merge his commit but time aint on my side