This includes various fixes and improvements from James Almer. Signed-off-by: James Almer <jamrial@gmail.com>