Minor tweaks, bug fix and refactoring