Adding preadvice codefrom desktop.
Need to merge in any changes from mac.