Cleaning up the code and restructured it