Fixed many small bugs and added some documentation