Add extra check for redirect-uri
Add comments, a bit of error-trapping, a bit of optimalization