Various fixes

David, could you have a glance as this before merging it into the repository.