Refactored all the code