tests for #451
fix #451 and optimize the generated code a bit