Reworking script to improve it and remove bugs.