Code review

Should be an acceptable subset of #386, if not, feel free to close.