working and tested implementation