Changes Jairaj wanted, plust a couple of bug fixes