Wip/code plugin rework layout fixes

See commit descriptions for more info. 

This PR is based on #368.