A bit of refactoring/fixes