Complete rewrite
Bug fixes