Many fixes to make code work