Fix permissions and some other bugs