Fixes various bugs, adds code comments, etc.