added more comments, corrected minor bugs