Fix #361 and minor cleanups