Logic bug + some cleaning up

@nealstewart