cleaned up code, added debugging options, documented scripting arguments