Got write commands working, and rearranged code a bit