code reformated
some bugs fixed