modified code to be more stable