repeated codes removed and automate unit tests