improved tests and fixed code