improved and tested