Incorporated tweaks from jamiebullock