added David's changes, and included changes from my branch

needs more testing, especially searching