Added patches from adamjcook branch