Fixes numerous bugs