Some code clean-up and refactor