Refactored lot of code to modulize stuff