moved utility function and cleaned up