fixing up more code formatting, consistency in formatting, refactoring code and simplify logic