Fix handling of named definitions in few situations
Add a bunch of tests