modified code to work better