refactoring the code to be smaller and neater

Please merge