Reorganized functions and some fixes.