Tweak order of link libraries, and add debug/release options to the build.
Merge updated upstream with local.