refactored data inserting function