Added better error logging and fixed a bug with 'expand'