handle empty code blocks better

Also, a bit of refactoring.