Refactroing and cleaning up the code