Improved the code. Now works fine.