Updated code. Works better!