Fix #276, Fix #386, Fix #382

Signed-off-by: Ross Allan <rallanpcl@gmail.com>