Multiple fixes and improvements

Replaces #18 and #23