refactored to better handle invalid callbacks and fix #52