Added functions. Also I rearranged the value function to make it easier to extend and know what is going on.