Implemented dig function

implemented the dig function and some minor changes to other places