cleaned and tested code