Fix bugs in #763

Please do not merge until Travis confirms that all tests are passed