Fix a number of bugs (details in pivotal)