Address #31

Same as #31 with some additional fixes.