Improve tests and fix code accordingly