modified code a bit to suite my need