comments added to the code, minor bugs corrected