Refactoring, clean, and fix callback function