added comments to code and fixed some functions