refactoring enqueue function