#3 fixing code duplicate and some small bugs.