fully working code, added working tests