final work and tested version