working version including tests