code style fixes and refactored as stated on comments