Minor edits

Code clean up, minor edits and reordering a few things. Added an output function which writes the chain to a file.