cleaned up and tested code