improve code, add a test