solved the problem with the help of mi ye
added comments
fixed stackoverflow exception