Add inline documentation and rewrite some code