Minor fixes and refactor