all necessary functions for prob 4 finished