improved functionality, added comments