put helper functions to helper.cpp and refactor