Fix #204 (Also some bug fixes for #315)