Function wrapping and small refactoring