First complete version; needs some cleanup, better error checking, and more idiomatic Go