Removed some of the common codes into a header file and refactored codes