Wrote documentation and fixed many potential problems.