Merge in a lot of changes from gaya plus add some security checks