Implementing remaining features from #40, with other minor improvements