Almost works now

Seperated testing and lib code, and had to change to support both
blocking and non-blocking mode.