Improvement of the problematic code by allysaalzona