fixed all bugs and warnings, added comments