Fixed duplicated code and some coding style