fixed bugs and added commenting