Refactoring and small feature improvements