added comments and min function