tidied up the code, fixed some buggs, globals are now signed, added comments