Refactor scala input output

This sits on #301, so only merge after that one's in.