added Phil's changes, fixed seg-fault