Function refactored