fixed delete bug, abstracted a write function

+ added a bunch of comments