Add test covrage and fix bug