Implementation for ticket 513

Please see individual commit messages for more info. This also contains the fix for ticket 549.