Various fixes to get this thing working

See commits for more information.