Use defonce and add other fixes