added comments and split up code