added more comments and general code cleanup