added better ways of doing this from jpwoodbu