Code cleanup

Removed deprecated "global $error" in various functions & general code
tidy up