Simplify code and more complete inline docs.