added space flagging, fixed a few bugs