Small fixes and refactoring