Fix a bug, improve performance analysis and add a test