A possible solution to this is always making sure he pees and poops before you crate him for the night, easier said than done I know. If he's taking too long and just faffing around, you need to step it up a bit and hassle him until he does his toilet. Have a patch of grass or bushes that he pees and poops on, and keep directing him back to that spot until he's evacuated. If he's not concentrating on what he's supposed to be doing, remind him with a command and lead him back to his toilet spot. Don't allow him to have an aimless leisure time, he's on the clock and he should know this. Then after he's done it, give him praise and a treat and let him sniff around for a while uninterrupted. This will hopefully teach him that if he wants to potter around outside at night, he has to go to the toilet first and get that out of the way.
Make it a rule that you don't talk to him or look at him once he's in his crate. If you have to clean up an accident, do it without acknowledging his existence as much as possible. He gets a little spike of gratification when you pay attention to him, so it's possible that he'll do whatever he can to get that spike of gratification when he's bored in his crate. By not making it exciting for him, you take away the incentive to play up when it's bedtime.
It sounds terrible, but another solution is to just let him sleep in his poop until morning a couple of times, then give him a shampoo. He'll learn very quickly not to go to the toilet in his bed if he doesn't have someone to clean it up instantly. He'll either go to the toilet when he's supposed to, or hold it until he gets let out the next day.