added ian's functions and fixed up to work