corrected bugs and added main function