refactoring the code and some formatting