Cleanup and small refactoring