Refactoring, fix little potential issues