Fix #636

Signed-off-by: Ross Allan <rallanpcl@gmail.com>
Fix #619, Fix #640

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