Reorganized code. Major changes