Re-factoring. Several fixed issues